package example;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import test.HelloWorldServiceLocator;
import test.HelloWorld_PortType;

/**
 * Created by Administrator on 2018/8/20 0020.
 */
public class HelloWorldClient {


    public static void main(String[] argv) {

    }

    @Test
    public void m1() {
        try {
            HelloWorldServiceLocator locator = new HelloWorldServiceLocator();
            HelloWorld_PortType helloWorl = locator.getHelloWorld();
            String res = helloWorl.sayHelloWorldFrom("guom");
            System.out.println(res);
        } catch (javax.xml.rpc.ServiceException ex) {
            ex.printStackTrace();
            System.out.println(ex.getMessage());
        } catch (java.rmi.RemoteException ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }

    @Test
    public void m2Xml() {
        try {
            // 服务地址 查看wsdl http://localhost:8080/web/readerService?wsdl
            URL url = new URL("http://localhost:8080/web/readerService");
            // 创建连接对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置参数
            // Http发送方式：POST必须大写
            connection.setRequestMethod("POST");
            // content-type
            connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
            // 设置输入输出，默认connection没有读写权限，
            connection.setDoInput(true);
            connection.setDoOutput(true);
            // 发送请求
            String soapXML = getXML("郭敏", "123456");
            OutputStream os = connection.getOutputStream();
            os.write(soapXML.getBytes());
            // 接收响应
            int responseCode = connection.getResponseCode();
            System.out.println("状态：" + responseCode);
            if (200 == responseCode) {
                response(connection);
            }
            os.close();
            System.out.println("----结束----");
        } catch (IOException e) {
            System.out.println("错误：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @Test
    public void m3Json() throws IOException {
        try {
            // 服务地址
            URL url = new URL("http://localhost:8080/App/app");
            // 创建连接对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置参数
            // Http发送方式：POST必须大写
            connection.setRequestMethod("POST");
            // content-type
            connection.setRequestProperty("content-type", "application/json;charset=UTF-8");
            // 设置输入输出，默认connection没有读写权限，
            connection.setDoInput(true);
            connection.setDoOutput(true);
            // 发送请求
            String json = "{\"name\":\"123\"}";
            OutputStream os = connection.getOutputStream();
            os.write(json.getBytes());
            // 接收响应
            int responseCode = connection.getResponseCode();
            System.out.println("状态：" + responseCode);
            if (200 == responseCode) {
                response(connection);
            }
            os.close();
            System.out.println("----结束----");
        } catch (IOException e) {
            System.out.println("错误：" + e.getMessage());
            e.printStackTrace();
        }

    }

    private static void response(HttpURLConnection connection) throws IOException {
        InputStream is = connection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        StringBuilder sb = new StringBuilder();
        String temp = null;
        while (null != (temp = br.readLine())) {
            sb.append(temp);
        }
        System.out.println(sb.toString());

        is.close();
        isr.close();
        br.close();
    }

    // 组织SOAP数据
    public static String getXML(String name, String pwd) {
        String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                + "<soap:Body>"
                + "<getName xmlns=\"http://WebXml.com.cn/\">"
                + "<name>" + name + "</name>"
                // + "<password>" + pwd + "</password>"
                + "</getName>"
                + "</soap:Body>"
                + "</soap:Envelope>";
        return soapXML;
    }
}
